package com.q.tqb.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.HtmlActivity;
import com.q.tqb.activity.PromtionActivity;
import com.q.tqb.activity.grab.GrabMapActivity;

import com.q.tqb.activity.grab.GrabSpeaking2Activity;

import com.q.tqb.activity.mine.JoinAgentActivity;
import com.q.tqb.activity.mine.MineTweeterIntroActivity;
import com.q.tqb.adapter.GrabRedEnvelopeAdapter;
import com.q.tqb.adapter.NewHomeAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.BannerInfo;
import com.q.tqb.model.NewHomeInfo;
import com.q.tqb.model.RedListData;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.GlideImageLoader;
import com.q.tqb.view.refresh.GRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class HomeFragment extends Fragment {


    private LinearLayout ll_tweeter;
    private LinearLayout ll_add_tweeter;
    private LinearLayout ll_home_map;

    private Banner banner;
    //    private GrabRedEnvelopeAdapter mGrabRedEnvelopeAdapter;
    private NewHomeAdapter mGrabRedEnvelopeAdapter;
    private ArrayList<NewHomeInfo> mRedListData;
    private RecyclerView rvHomeGrab;
    private SwipeRefreshLayout swipeLayout;
    private int currentPageNo;
    private int isUserAgent;


    public HomeFragment() {

    }


    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        ll_tweeter = view.findViewById(R.id.ll_tweeter);
        ll_add_tweeter = view.findViewById(R.id.ll_add_tweeter);
        ll_home_map = view.findViewById(R.id.ll_home_map);

        banner = view.findViewById(R.id.banner);
        rvHomeGrab = view.findViewById(R.id.rv_home_grab);
        swipeLayout = view.findViewById(R.id.swipeLayout);
        initView();
        init();
        return view;
    }

    private void initView() {
        getBannerData();

        ll_home_map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), GrabMapActivity.class);
                startActivity(intent);
            }
        });
        ll_tweeter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // onTweeterClick.onTweeter();
//                Intent intent = new Intent(getActivity(), PromtionActivity.class);
//                startActivity(intent);
                Intent intent = new Intent(getActivity(), JoinAgentActivity.class);
                startActivity(intent);
            }
        });

        ll_add_tweeter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isUserAgent = (int) SPUtil.get(getActivity(), "isUserAgent", 0);
                Intent intent;
                if (isUserAgent == 1) {
                    intent = new Intent(getActivity(), HtmlActivity.class);
                    intent.putExtra("url", AppConst.URL_RULE);
                    intent.putExtra("title", getString(R.string.home_tweeter_guide));
                    startActivity(intent);
                } else {
                    intent = new Intent(getActivity(), MineTweeterIntroActivity.class);
                    startActivityForResult(intent, 55);
                }
            }
        });
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeLayout.setRefreshing(true);
                getListData();
            }
        });

    }

    private void init() {
        mRedListData = new ArrayList<>();

        mGrabRedEnvelopeAdapter = new NewHomeAdapter(R.layout.item_home_grab_red, R.layout.item_newhome, mRedListData);

        mGrabRedEnvelopeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent;
                if (!mRedListData.get(position).isHeader) {
                    if (mRedListData.get(position).t.getPacket_type() == 1) {
                        intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                        intent.putExtra("rid", mRedListData.get(position).t.getRid());
                        intent.putExtra("uid", mRedListData.get(position).t.getUid());
                        startActivity(intent);
                    } else if (mRedListData.get(position).t.getPacket_type() == 2) {
                        intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                        intent.putExtra("rid", mRedListData.get(position).t.getRid());
                        intent.putExtra("uid", mRedListData.get(position).t.getUid());
                        startActivity(intent);
                    }else if (mRedListData.get(position).t.getPacket_type() == 3) {
                        intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                        intent.putExtra("rid", mRedListData.get(position).t.getRid());
                        intent.putExtra("uid", mRedListData.get(position).t.getUid());
                        intent.putExtra("type", 1);
                        startActivity(intent);
                    }
                }
            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        rvHomeGrab.setLayoutManager(gridLayoutManager);
        rvHomeGrab.setAdapter(mGrabRedEnvelopeAdapter);
        getListData();
    }

    private void getBannerData() {
        OkHttpUtil.getInstance().post(getActivity(), AppConst.URL_BANNER, null, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                Gson gson = new Gson();
                final ArrayList<BannerInfo> bannerInfos = gson.fromJson(element, new TypeToken<ArrayList<BannerInfo>>() {
                }.getType());

                banner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        if (bannerInfos.get(position).getLink_type() == 2) {
                            Intent intent = new Intent(getActivity(), HtmlActivity.class);
                            intent.putExtra("url", bannerInfos.get(position).getBanner_link());
                            startActivity(intent);
                        }
                        if (bannerInfos.get(position).getLink_type() == 1) {
                            Intent intent = new Intent(getActivity(), PromtionActivity.class);
                            startActivity(intent);
                        }
                    }
                });

                banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                banner.setImageLoader(new GlideImageLoader());
                banner.setImages(bannerInfos);
                banner.isAutoPlay(true);
                banner.setDelayTime(3000);
                banner.setIndicatorGravity(BannerConfig.CENTER);
                banner.start();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
            }
        });
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
//        map.put("pageNo", currentPageNo + "");
//        map.put("pageSize ", 10 + "");
        OkHttpUtil.getInstance().post(getActivity(), AppConst.URL_GET_HOME_PACKET, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<NewHomeInfo> redListData = gson.fromJson(element, new TypeToken<ArrayList<NewHomeInfo>>() {
                }.getType());

                swipeLayout.setRefreshing(false);
                mRedListData.clear();
                mRedListData.addAll(paseData(redListData));
                mGrabRedEnvelopeAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                swipeLayout.setRefreshing(false);
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private ArrayList<NewHomeInfo> paseData(ArrayList<NewHomeInfo> redListData) {
        ArrayList<NewHomeInfo> newList = new ArrayList<>();
        for (NewHomeInfo bean : redListData) {
            newList.add(new NewHomeInfo(true, bean.getPacket_name()));
            for (RedListData r : bean.getPacket()) {
                newList.add(new NewHomeInfo(r));
            }
        }
        return newList;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 55 && resultCode == Activity.RESULT_OK) {
            isUserAgent = 1;
            Intent intent1 = new Intent(getActivity(), HtmlActivity.class);
            intent1.putExtra("url", AppConst.URL_RULE);
            intent1.putExtra("title", getString(R.string.home_tweeter_guide));
            startActivity(intent1);
        }
    }


    private OnTweeterClick onTweeterClick;


    public void setOnTweeterClick(OnTweeterClick onButtonClick) {
        this.onTweeterClick = onButtonClick;
    }


    public interface OnTweeterClick {
        void onTweeter();
    }
}
